﻿using System;
using System.Collections.Generic;
using Eff.Utils;
using Redmine.Core.Entities;
using Redmine.Core.UnitOfWork;
using UnitOfWork;
using UnitOfWork.Validation;

namespace Redmine.Core.Repositories
{
    public class UserRepository : RepositoryBase<User, DbDataContext>, IUserRepository
    {
        public UserRepository(IDatabaseFactory<DbDataContext> databaseFactory)
            : base(databaseFactory)
        {

        }

        public override void Add(User entity)
        {
            entity.CreatedOn = entity.UpdatedOn = entity.LastLoginTime = DateTime.Now;
            base.Add(entity);
        }

        public override void Update(User entity)
        {
            entity.UpdatedOn = DateTime.Now;
            base.Update(entity);
        }

        public User Anonymous
        {
            get { return Singleton<AnonymousUser>.Instance; }
        }

        private User _current;
        public User Current
        {
            get { return _current ?? (_current = Anonymous); }
            set { _current = value; }
        }

        public bool Login(string userName, string password)
        {
            throw new NotImplementedException();
        }
    }
    public interface IUserRepository : IRepository<User>
    {
        /// <summary>
        ///  获取匿名用户
        /// </summary>
        User Anonymous { get; }
        /// <summary>
        /// 获取当前用户
        /// </summary>
        User Current { get; set; }

        bool Login(string userName, string password);
    }
}
